package com.iespring.util;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.google.gson.Gson;
import com.iespring.bean.admin.user.entity.MallUser;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Random;

/**
 * Auth: 杨宇健
 * Time: 2023/1/2 14:13
 * Description: 短信发送服务
 **/
public class SMSUtils {

    static Random random = new Random();
    //map{code : [ip, time]}
    private static HashMap<String, Object[]> codePhoneTimeMap = new HashMap<>();


    public static void SMSSend( String phone ) throws JsonProcessingException {
        //TODO 杨宇健 上线后删掉
        // 为了避免频繁发送短信。设置一个defualt属性
        codePhoneTimeMap.put("111111",new Object[]{"13414981493", new Date()});
;
        Random random = new Random();
        String code = Integer.toString(100000 + random.nextInt(900000));
        Object[] ipTimeObject =  new Object[]{phone, new Date()};

        //存进map中
        codePhoneTimeMap.put(code, ipTimeObject);

        HashMap<String, String> map = new HashMap<>();
        map.put("code", code);
        Gson gson = new Gson();
        String codeJson = gson.toJson(map);
        sms(phone, codeJson);
    }

    //删除过期的map
    private static void removeTimeOutCode() {
        long time = new Date().getTime();
        codePhoneTimeMap.entrySet().stream().forEach( map -> {
            Date inputTime = (Date) map.getValue()[1];
            if (time - (inputTime.getTime()) > 5*60*1000) {
                codePhoneTimeMap.remove(map.getKey());
            }
        });
    }

    //发送短信
    private static void  sms(String phone, String codeJson) throws JsonProcessingException {
        String accessKeyId = "LTAI5t8gpxPTCR6W58RnZq4u";
        String accessKeySecret = "mVPbpbxia0JQotb7HyJAREV8QUuq8h";
        String signName = "stone4j";
        String templateCode = "SMS_173765187";


        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", codeJson);
        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }

    //验证输入的验证码是否正确
    public static boolean validation(String code, String phone){
        //把过期的map给删除掉
        removeTimeOutCode();
        Object[] phoneTime = codePhoneTimeMap.get(code);
        String cachePhone = (String) phoneTime[0];
        if ( cachePhone.equals(phone)) {
            return true;
        }
        return false;
    }
}
